<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class makul extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_makul');
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
    }

    // halaman list data
    public function index() {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/makul/index.html");
        // data session
        $search = $this->tsession->userdata('search_makul');
        $this->smarty->assign('search', $search);
        // params
        $makul_nm = !empty($search['makul_nm']) ? "%" . $search['makul_nm'] . "%" : "%";
        //--
        $params = array($makul_nm);
        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("master/makul/index/");
        $config['total_rows'] = $this->m_makul->get_total_data_makul($params);
        $config['uri_segment'] = 4;
        $config['per_page'] = 20;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(4, 0) + 1;
        $end = $this->uri->segment(4, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array($makul_nm, ($start - 1), $config['per_page']);
        $result = $this->m_makul->get_data_makul($params);
        $this->smarty->assign('rs_id', $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses cari
    public function search_process() {
        // set page rules
        $this->_set_page_rule("R");
        //--
        if ($this->input->post('save') == 'Cari') {
            // params
            $makul_nm = $this->input->post('makul_nm');
            //--
            $params = array("makul_nm" => $makul_nm);
            // set session
            $this->tsession->set_userdata('search_makul', $params);
        } else {
            // unset session
            $this->tsession->unset_userdata('search_makul');
        }
        // redirect
        redirect('master/makul');
    }

    // halaman tambah data
    public function add() {
        // set page rules
        $this->_set_page_rule("C");
        // set template content
        $this->smarty->assign("template_content", "master/makul/add.html");
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses tambah data
    public function add_process() {
        // set page rules
        $this->_set_page_rule("C");
        // cek input
        $this->tnotification->set_rules('makul_kode', 'Kode Mata Kuliah', 'trim|required|maxlength[10]');
        $this->tnotification->set_rules('makul_nm', 'Nama Mata Kuliah', 'trim|required|maxlength[100]');
        $this->tnotification->set_rules('makul_sks', 'Jumlah SKS', 'trim|required|maxlength[10]|numeric');
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $makul_kode = $this->input->post('makul_kode');
            $makul_nm = $this->input->post('makul_nm');
            $makul_sks = $this->input->post('makul_sks');
            //--
            $params = array(strtoupper($makul_kode), strtoupper($makul_nm), $makul_sks, $this->com_user['user_id']);
            // input data
            if ($this->m_makul->insert_data_makul($params)) {
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect('master/makul/add');
    }

    // halaman edit data
    public function edit($makul_kd = "") {
        // set page rules
        $this->_set_page_rule("U");
        // set template content
        $this->smarty->assign("template_content", "master/makul/edit.html");
        // data
        $result = $this->m_makul->get_detail_data_makul($makul_kd);
        $this->smarty->assign('result', $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses edit data
    public function edit_process() {
        // set page rules
        $this->_set_page_rule("U");
        // cek input
        $this->tnotification->set_rules('makul_kode', 'Kode Mata Kuliah', 'trim|required|maxlength[10]');
        $this->tnotification->set_rules('makul_nm', 'Nama Mata Kuliah', 'trim|required|maxlength[100]');
        $this->tnotification->set_rules('makul_sks', 'Jumlah SKS', 'trim|required|maxlength[10]|numeric');
        $this->tnotification->set_rules('makul_kd', 'Kode Mata Kuliah', 'required');
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $makul_kode = $this->input->post('makul_kode');
            $makul_nm = $this->input->post('makul_nm');
            $makul_sks = $this->input->post('makul_sks');
            $makul_kd = $this->input->post('makul_kd');
            //--
            $params = array(
                strtoupper($makul_kode), strtoupper($makul_nm), $makul_sks,
                $this->com_user['user_id'], $makul_kd
            );
            // edit data
            if ($this->m_makul->update_data_makul($params)) {
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect('master/makul/edit/' . $this->input->post('makul_kd'));
    }

    // halaman hapus data
    public function delete($makul_kd = "") {
        // set page rules
        $this->_set_page_rule("D");
        // set template content
        $this->smarty->assign("template_content", "master/makul/delete.html");
        // data
        $result = $this->m_makul->get_detail_data_makul($makul_kd);
        $this->smarty->assign('result', $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses hapus
    public function delete_process() {
        // set page rules
        $this->_set_page_rule("D");
        // cek input
        $this->tnotification->set_rules('makul_kd', 'Kode Mata Kuliah', 'required');
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $makul_kd = $this->input->post('makul_kd');
            // edit data
            if ($this->m_makul->delete_data_makul($makul_kd)) {
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil dihapus");
                // redirect
                redirect('master/makul');
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal dihapus");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal dihapus");
        }
        // default redirect
        redirect('master/makul/delete/' . $this->input->post('makul_kd'));
    }

}